package com.cskaoyan.gateway.controller.order;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@CrossOrigin(value = "*", allowCredentials = "true")
@RestController
@RequestMapping("order")
public class OrderController {

    @Reference(timeout = 30000, check = false,retries = 0)
    private OrderQueryService orderQueryService;

    @Reference(timeout = 30000, check = false,retries = 0)
    private OrderCoreService orderCoreService;

    /**
     * 创建订单
     *
     * @param request
     * @param servletRequest
     * @return
     */
    @PostMapping("create")
    public ResponseData createOrder(@RequestBody Map<String, Object> request, HttpServletRequest servletRequest) {

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        //这里的userId是user表的id
        Integer userId = Integer.parseInt(object.get("uid").toString());

        //封装请求参数
        request.put("userId",userId);

        //远程调用
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(request);

        if (createOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(createOrderResponse,"success");
        }
        return new ResponseUtil<>().setErrorMsg(createOrderResponse.getMsg());
    }

    /**
     * 获取当前用户的全部订单信息
     * @param request
     * @return
     */


    /**
     * 获取订单详情
     *
     * @param orderId
     * @param servletRequest
     * @return
     */


    /**
     * 取消订单
     * @param
     * @return
     */


    /**
     * 删除订单
     * @param orderId
     * @return
     */

}



